{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "659c25fd-2a10-4010-ae4c-bf6c42c59a6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "from starvector.model.starvector_arch import StarVectorForCausalLM\n",
    "from starvector.data.util import process_and_rasterize_svg\n",
    "import torch\n",
    "\n",
    "model_name = \"starvector/starvector-1b-im2svg\"\n",
    "#model_name = \"starvector/starvector-8b-im2svg\"\n",
    "\n",
    "starvector = StarVectorForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)\n",
    "\n",
    "starvector.cuda()\n",
    "starvector.eval()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a3ae9d8-7e7c-4eba-b830-4f45d68f7b5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "image_pil = Image.open('../assets/examples/sample-18.png')\n",
    "\n",
    "image = starvector.process_images([image_pil])[0].cuda()\n",
    "batch = {\"image\": image}\n",
    "\n",
    "raw_svg = starvector.generate_im2svg(batch, max_length=5000)[0]\n",
    "svg, raster_image = process_and_rasterize_svg(raw_svg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a100757-c512-4e4b-a05d-f5a74fbf9133",
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import SVG, display\n",
    "\n",
    "display(image_pil)\n",
    "display(SVG(svg))\n",
    "display(raster_image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b13dd1af-d579-452a-96f8-f0fae5289d24",
   "metadata": {},
   "outputs": [],
   "source": [
    "del starvector\n",
    "torch.cuda.empty_cache()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6cf1d4d9-2798-4b67-9942-7b3a5cf03122",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
